/*
 * 特别注意：
 * 1.目前数据只能支持级联到县，所以初始化select元素不建议超过3个
 * 2.请确保select元素中有初始化的option，以避免一些小问题
 * 3.IE9可用但会出现一些小问题，所以不建议使用
 * 4.IE8及以下不兼容，因为window.addEventListener的原因
 */
function setRegionData(url, element) {
  var selectArray = element.querySelectorAll("select");

  var provinceOption = selectArray[0] ? selectArray[0].innerHTML : null;
  var cityOption = selectArray[1] ? selectArray[1].innerHTML : null;
  var countyOption = selectArray[2] ? selectArray[2].innerHTML : null;
  var townOption = selectArray[3] ? selectArray[3].innerHTML : null;
  var villageOption = selectArray[4] ? selectArray[4].innerHTML : null;

  if (!window.XMLHttpRequest && !window.ActiveXObject) {
    alert("您的浏览器不支持AJAX，建议您更新到最新版或者换一个流行的浏览器试试");
    return;
  }

  var xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.send();
  xhr.onreadystatechange = function () {
    if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
      var data = JSON.parse(xhr.responseText);

      // 省份初始化
      if (selectArray[0]) {
        for (var provinceCode in data["86"]) {
          selectArray[0].add(new Option(data["86"][provinceCode], provinceCode));
        }
      }

      // 省-市级联
      if (selectArray[1]) {
        selectArray[0].addEventListener("change", function () {
          selectArray[1].innerHTML = cityOption;
          if (selectArray[2]) selectArray[2].innerHTML = countyOption;
          if (selectArray[3]) selectArray[3].innerHTML = townOption;
          if (selectArray[4]) selectArray[4].innerHTML = villageOption;

          for (var provinceCode in data) {
            if (selectArray[0].value === provinceCode) {
              for (var cityCode in data[provinceCode]) {
                selectArray[1].add(new Option(data[provinceCode][cityCode],
                    cityCode));
              }
            }

          }
        });
      }

      // 市-县级联
      if (selectArray[2]) {
        selectArray[1].addEventListener("change", function () {
          selectArray[2].innerHTML = countyOption;
          if (selectArray[3]) selectArray[3].innerHTML = townOption;
          if (selectArray[4]) selectArray[4].innerHTML = villageOption;

          for (var cityCode in data) {
            if (selectArray[1].value === cityCode) {
              for (var countyCode in data[cityCode]) {
                selectArray[2].add(new Option(data[cityCode][countyCode],
                    countyCode));
              }
            }
          }
        });
      }

      // 县-镇级联
      if (selectArray[3]) {
        selectArray[2].addEventListener("change", function () {
          selectArray[3].innerHTML = townOption;
          if (selectArray[4]) selectArray[4].innerHTML = villageOption;

          for (var countyCode in data) {
            if (selectArray[2].value === countyCode) {
              for (var townCode in data[countyCode]) {
                selectArray[3].add(new Option(data[countyCode][townCode],
                    townCode));
              }
            }
          }
        });
      }

      // 镇-村级联
      if (selectArray[4]) {
        selectArray[3].addEventListener("change", function () {
          selectArray[4].innerHTML = villageOption;

          for (var townCode in data) {
            if (selectArray[3].value === townCode) {
              for (var villageCode in data[townCode]) {
                selectArray[4].add(new Option(data[townCode][villageCode],
                    villageCode));
              }
            }
          }
        });
      }
    }
  };
}
